#include <vector>
using namespace std;
class Solution
{
public:
    bool in_time(int speed, vector<int> &piles, int h)
    {
        int res = 0;
        for (auto n : piles)
        {
            res += n / speed;
            if (n % speed)
                res++;
            if (res > h)
                return false;
        }
        return true;
    }
    // 暴力: 遍历每一个速度 然后尝试
    // 二分优化: 确定速度上界: 最大框 和速度下界 1 然后二分去调整速度,得出最小能吃完的速度
    int minEatingSpeed(vector<int> &piles, int h)
    {
        int max_nums(0);
        for (auto e : piles)
            max_nums = max(e, max_nums);
        int l(1), r(max_nums);
        while (l < r)
        {
            int mid_speed = (l + r) >> 1;
            if (!in_time(mid_speed, piles, h))
                l = mid_speed + 1;
            else
                r = mid_speed;
        }
        return r;
    }
};
//2023/9/6
class Solution {
public:
    int num_to_speed(int num,vector<int>& piles){
        int h=0;
        for(auto e:piles){
            h+=(e+num-1)/num;
        }
        return h;
    }
    int minEatingSpeed(vector<int>& piles, int h) {
        int low=1,max=0;
        for(auto e :piles){
            if(e<low)    low=e;
            if(e>max)    max=e;
        }
        while(low<max){
            int mid=(low+max)>>1;
            if(num_to_speed(mid,piles)>h){
                low=mid+1;
            }
            else
                max=mid;
        }
        return low;
    }
};